layui.use(['admin'], function () {
    let $ = layui.$
        , admin = layui.admin
        , setter = layui.setter
        , form = layui.form
        , laytpl = layui.laytpl
        , controller = '/admin/systemConfig.config';

    let layerIndex = '';
    let layType = '';
    let layRule = '';
    let layGroup = '';
    let getTpl = '';
    let html = '';
    layui.data.sendParams = function (params) {
        layerIndex = params.lay_index;
        layType = params.type;
        layRule = params.rule;
        layGroup = params.group;

        let groupList = params.groupList;
        let ruleList = params.ruleList;
        let typeList = params.typeList;

        getTpl = sysConfigLAYTpl.innerHTML;
        html = laytpl(getTpl).render({
            groupList: groupList,
            layGroup: layGroup
        });
        $('#sysConfigLAYView').html(html);

        getTpl = sysConfigLAYRule.innerHTML;
        html = laytpl(getTpl).render({
            ruleList: ruleList,
            layRule: layRule
        });
        $('#sysConfigLAYRuleView').html(html);

        getTpl = sysConfigLAYType.innerHTML;
        html = laytpl(getTpl).render({
            typeList: typeList,
            layType: layType
        });
        $('#sysConfigLAYTypeView').html(html);
        form.render('select'); //重载表格

    }

    form.on('select(configType)', function(data){
        let value = data.value;
        console.log(value);
        let arr = ['radio', 'switch', 'checkbox', 'select', 'xm-select', 'xm-selects'];
        if(arr.indexOf(value) > -1){
            $("#configContentDiv").show();
        }else{
            $("#configContentDiv").hide();
        }
    })

    form.verify({
        varName: function (value) {
            if (value.length < 1) {
                return '必填项不能为空！';
            }
            if (!new RegExp("^[a-zA-Z_][a-zA-Z0-9_]*$").test(value)) {
                return '变量名只能为数字、字母、下划线的组合，且不能以数字开头！';
            }
        },
    });

    /**
     * 添加数据
     */
    form.on('submit(laySubmitAdd)', function(data){
        let field = data.field; //获取提交的字段
        // 提交 Ajax 成功后，关闭当前弹层并重载表格
        admin.req({
            url: setter.request.domainName + controller +'/add' //实际使用请改成服务端真实接口
            , data: field
            , type: 'POST'
            , loading: true // 是否开启加载框
            , done: function(res){
                layui.table.reload('sysConfigTable'); //重载表格
                admin.message({
                    title: '提交成功'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1500
                })
                layer.close(layerIndex); //执行关闭
            }
        })
    });

    /**
     * 编辑数据
     */
    form.on('submit(laySubmitEdit)', function(data){
        let field = data.field; //获取提交的字段
        // 提交 Ajax 成功后，关闭当前弹层并重载表格
        admin.req({
            url: setter.request.domainName + controller +'/edit' //实际使用请改成服务端真实接口
            , data: field
            , type: 'POST'
            , loading: true // 是否开启加载框
            , done: function(res){
                layui.table.reload('sysConfigTable'); //重载表格
                admin.message({
                    title: '提交成功'
                    ,offset: '15px'
                    ,icon: 1
                    ,time: 1500
                })
                layer.close(layerIndex); //执行关闭
            }
        })
    });
})
